package com.zmx.patientend.view.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.zmx.patientend.BaseActivity;
import com.zmx.patientend.R;
import com.zmx.patientend.adapter.EvaluationAdapter;
import com.zmx.patientend.okhttp.OkHttp3ClientManager;
import com.zmx.patientend.pojo.Evaluation;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 程序猿：胖胖祥
 * 时 间：2020/10/29  11:45
 * 功 能：评论列表
 */
public class CommentsListActivity extends BaseActivity {

    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    private String did;

    private RefreshLayout mRefreshLayout;
    private List<Evaluation> lists;
    private EvaluationAdapter adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_comments_list;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        setToolbar(R.id.tool_bar);
        did = this.getIntent().getStringExtra("did");

        lists = new ArrayList<>();
        adapter = new EvaluationAdapter(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView.setAdapter(adapter);

        // 开启自动加载功能
        mRefreshLayout = findViewById(R.id.refreshLayout);
        //触发自动刷新
        mRefreshLayout.autoRefresh();
        mRefreshLayout.setEnableAutoLoadMore(true);
        mRefreshLayout.setOnRefreshListener(refreshLayout -> {

            getComment();

        });

        mRefreshLayout.setOnLoadMoreListener(refreshLayout -> {

            //刷新加载
            mRefreshLayout.finishLoadMore(true);

        });

    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what){

                case 1:

                    try {
                        lists.clear();
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());

                        if (jsonObject.getInt("code") == 1){

                            JSONArray data = jsonObject.getJSONArray("data");
                            Gson g = new Gson();

                            for (int i = 0;i<data.length();i++){

                                Evaluation evaluation = g.fromJson(data.getJSONObject(i).toString(), Evaluation.class);
                                lists.add(evaluation);

                            }

                            adapter.setListAndNotifyDataSetChanged(lists);       //刷新加载
                            mRefreshLayout.finishRefresh(true);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    break;

            }

        }
    };

    public void getComment() {

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", "");
        params.put("data[doctor_member_id]", did);
        params.put("data[row]", 1 + "");
        params.put("data[number]", 10 + "");
        OkHttp3ClientManager.getInstance().NetworkRequestMode("http://zmx.123xk.cn/admin/api.doctor/getComment", params, handler, 1, 404);

    }


}
